<?php

/**
 * BaseFeed base model
 *
 * PHP version 5
 *
 * @category   Web
 * @package    Data2People
 * @subpackage Models
 * @author     Mike Bevz <mike.bevz@data2people>
 * @copyright  2007-2008 (c) Mike Bevz
 * @license    The BSD License http://data2people.com/license.txt
 * @version    SVN: $Id: BaseNews.php 13 2008-12-01 16:16:50Z michael.bevz $
 *
 *
 */
/**
 * Includes
 */
require_once 'Doctrine/Record.php';
require_once 'TimestampListener.php';
require_once 'Feed.php';


abstract class BaseFeed extends Doctrine_Record
{
    /**
    * Sets table definitions
    *
    * @return void
    */
    public function setTableDefinition()
    {
        $this->setTableName('feeds');
        $this->hasColumn('id', 'integer', 4, array('primary' => true,
                                                   'autoincrement' => true,
                                                   'unsigned' => true));
        $this->hasColumn('title', 'string', 255);
        $this->hasColumn('link', 'string', 255, array('unique'=> true));
        $this->hasColumn('description', 'string', 2147483647);
        $this->hasColumn('pubdate', 'timestamp', array('format'=> 'Y-m-d H:i:s'));
        $this->hasColumn('language', 'string', 255);
        $this->hasColumn('category', 'string', 255);
        
    }
    
   /**
    * Sets up relations
    *
    * @return void
    */
    public function setUp()
    {
        $this->hasMany('News', array('local' => 'id',
                                     'foreign' => 'feed_id'));
        
        $this->actAs('Timestampable',
               array('created_at' =>  array('name'  =>  'created_at',
                                            'type'    =>  'timestamp',
                                            'format'  =>  'Y-m-d H:i:s',
                                            'disabled' => false,
                                            'options' =>  array()),
                     'updated_at' =>  array('name'  =>  'updated_at',
                                            'type'    =>  'timestamp',
                                            'format'  =>  'Y-m-d H:i:s',
                                            'disabled' => false,
                                            'options' =>  array())));
        
       
        $this->setListener(new TimestampListener());
    }
    
    /**
     * Llink is unique identifier so we cancel insert if the link exists
     *
     * @param Doctrine_Event $event
     *
     * @return void
     */
    public function preSave(Doctrine_Event $event)
    {
        $feeditem = new Feed();
        $feeds = $feeditem->getByLink($this->link);
        if (count($feeds) > 0) {
            $event->skipOperation();
        }
    }

}